from PyQt5 import QtCore, QtGui, QtWidgets
import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMessageBox, QHeaderView, QApplication

import mysq
from ui.biao import Ui_Frame as bia
from mysq import *


class zi(QtWidgets.QMainWindow, bia):
    def __init__(self):
        super(zi, self).__init__()
        self.setupUi(self)
        self.FileDir_ru=''
        self.FileDir_chu = ''
        self.InitSlots()
        self.flage_ru=False
        self.flage_cu=False

    def InitSlots(self):
        self.box()
        self.pushButton_file.clicked.connect(self.open_file) #打开导入的excel
        self.pushButton_site.clicked.connect(self.open_site) #选择excel导出的地址
        self.pushButton_ru.clicked.connect(self.shuru) #导人
        self.pushButton_lock.clicked.connect(self.lock) #显示信息
        self.pushButton_2.clicked.connect(self.shuaxin) #刷新
        self.pushButton_chu.clicked.connect(self.shuchu) #导出
        self.pushButton.clicked.connect(self.chaxun)#查询
    #查询场地得信息
    def chaxun(self):
        a1=self.comboBox.currentText()
        row, vol, result = mysq.cha(a1)
        self.tableWidget.setRowCount(0)
        self.tableWidget.setRowCount(row)  # 设置表格行数
        self.tableWidget.setColumnCount(vol)  # 设置表格列数

        self.tableWidget.setHorizontalHeaderLabels(['比赛序号', '中轮次', '轮次', '场地', '场地号', '级别', '日期',
                                                    '青方编号', '青方名字', '青方单位', '红方编号', '红方名字',
                                                    '红方单位', '比赛状态', '获胜者', '获胜方式', '青方分数,',
                                                    '红方分数', '青方扣分', '红方扣分', '青方得分1', '红方得分1',
                                                    '青方扣分1',
                                                    '红方扣分1', '青方得分2', '红方得分2', '青方扣分2', '红方扣分2',
                                                    '青方得分3', '红方得分3', '青方扣分3', '红方扣分3', '青方得分4',
                                                    '红方得分4', '青方扣分4', '红方扣分4', '青方赢数量', '红方赢数量'])
        for i in range(row):
            for j in range(vol):
                item = QtWidgets.QTableWidgetItem(str(result[i][j]))
                self.tableWidget.setItem(i, j, item)
        self.tableWidget.resizeColumnsToContents()  # 使列宽跟随内容改变
        self.tableWidget.resizeRowsToContents()  # 使行高跟随内容改变
        self.tableWidget.setAlternatingRowColors(True)  #
        self.tableWidget.horizontalHeader().setStretchLastSection(True)

    #出现选择的信息
    def box(self):
        data=mysq.chang()
        for item in data:
            self.comboBox.addItem(item)

    #输出excel文件
    def shuchu(self):
        if self.flage_cu==True:
            a=mysq.daochu(self.FileDir_chu)
            if a==1:
                QMessageBox.information(self, "提示", "导出成功", QMessageBox.Ok)
                self.lineEdit_2.setText('')
        else:
            QMessageBox.information(self, "提示", "请先选择文件", QMessageBox.Ok)
    #刷新
    def shuaxin(self):
        self.lock()
    # 选择导入文件
    def open_file(self):
        fileInfo = QtWidgets.QFileDialog.getOpenFileName(None, "选择文件", "")  # 选择文件
        self.FileDir_ru = fileInfo[0]
        self.lineEdit.setText(fileInfo[0])
        self.flage_ru=True
    #选择导出地址
    def open_site(self):
        fileInfo = QtWidgets.QFileDialog.getExistingDirectory(None, "选取文件夹")
        self.FileDir_chu = fileInfo
        self.lineEdit_2.setText(fileInfo)
        self.flage_cu=True
    #显示信息
    def lock(self):
        row, vol, result = mysq.kais()
        self.tableWidget.setRowCount(0)
        self.tableWidget.setRowCount(row)  # 设置表格行数
        self.tableWidget.setColumnCount(vol)  # 设置表格列数

        self.tableWidget.setHorizontalHeaderLabels(['比赛序号','中轮次','轮次','场地','场地号','级别','日期',
'青方编号','青方名字','青方单位','红方编号','红方名字','红方单位','比赛状态','获胜者','获胜方式','青方分数,','红方分数','青方扣分','红方扣分','青方得分1','红方得分1','青方扣分1',
'红方扣分1','青方得分2','红方得分2','青方扣分2','红方扣分2','青方得分3','红方得分3','青方扣分3','红方扣分3','青方得分4','红方得分4','青方扣分4','红方扣分4','青方赢数量','红方赢数量'])
        for i in range(row):
            for j in range(vol):
                item = QtWidgets.QTableWidgetItem(str(result[i][j]))
                self.tableWidget.setItem(i, j, item)
        self.tableWidget.resizeColumnsToContents()  # 使列宽跟随内容改变
        self.tableWidget.resizeRowsToContents()  # 使行高跟随内容改变
        self.tableWidget.setAlternatingRowColors(True)  #
        self.tableWidget.horizontalHeader().setStretchLastSection(True)
    #导入
    def shuru(self):
        if self.flage_ru==True:
            a=mysq.shuru_2(self.FileDir_ru)
            self.lineEdit_2.setText('')
            if a==0:
                QMessageBox.information(self, "提示", "导入成功", QMessageBox.Ok)
                self.lineEdit.setText('')
        else:
            QMessageBox.information(self, "提示", "请先选择文件", QMessageBox.Ok)




if __name__ == '__main__':

    QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    # 适应高DPI设备
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    # 解决图片在不同分辨率显示模糊问题

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = zi()
    MainWindow.show()
    sys.exit(app.exec_())
